package com.wanghaoran.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.wanghaoran.dto.AssetsInsertDTO;
import com.wanghaoran.dto.AssetsPageDTO;
import com.wanghaoran.dto.AssetsUpdateDTO;
import com.wanghaoran.dto.PageDTO;
import com.wanghaoran.entity.Assets;
import com.wanghaoran.excel.AssetsExcel;
import com.wanghaoran.excel.SchoolExcel;
import com.wanghaoran.result.Result;
import com.wanghaoran.service.AssetsService;
import com.wanghaoran.util.EasyExcelUtil;
import com.wanghaoran.vo.AssetsVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.data.domain.PageImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wanghaoran
 */
@Tag(name = "资产模块")
@RestController
@RequestMapping("api/v1/assets")
public class AssetsController {

    @Resource
    private AssetsService assetsService;

    @Operation(summary = "新增 - 单条新增")
    @PostMapping("insert")
    public Result<Integer> insert(@RequestBody @Validated AssetsInsertDTO dto) {
        return new Result<>(assetsService.insert(dto));
    }

    @Operation(summary = "查询 - 单条查询")
    @GetMapping("select/{id}")
    public Result<Assets> select(@PathVariable("id") Long id) {
        return new Result<>(assetsService.select(id));
    }

    @Operation(summary = "查询 - 全部记录")
    @GetMapping("list")
    public Result<List<AssetsVO>> list() {
        return new Result<>(assetsService.list());
    }

    @Operation(summary = "查询 - 分页查询")
    @GetMapping("page")
    public Result<PageInfo<Assets>> page(@Validated AssetsPageDTO dto) {
        return new Result<>(assetsService.page(dto));
    }

    @Operation(summary = "修改 - 单条修改")
    @PutMapping("update")
    public Result<Integer> update(@RequestBody @Validated AssetsUpdateDTO dto) {
        return new Result<>(assetsService.update(dto));
    }

    @Operation(summary = "删除 - 单条删除")
    @DeleteMapping("delete/{id}")
    public Result<Integer> delete(@PathVariable("id") Long id) {
        return new Result<>(assetsService.delete(id));
    }

    @Operation(summary = "删除 - 批量删除")
    @DeleteMapping("deleteBatch")
    public Result<Integer> deleteBatch(@RequestParam("ids") List<Long> ids) {
        return new Result<>(assetsService.deleteBatch(ids));
    }

    @Operation(summary = "查询 - 报表打印")
    @SneakyThrows
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp) {
        // 导出Excel文件
        EasyExcelUtil.download(resp, "资产统计表", assetsService.getExcelData());
    }

    @Operation(summary = "上传 - 资产图片")
    @PostMapping("/uploadPicture/{id}")
    public Result<String> uploadPicture(@RequestParam("pictureFile") MultipartFile pictureFile,
                                        @PathVariable("id") Long id) {
        return new Result<>(assetsService.uploadPicture(pictureFile, id));
    }
}
